<?php

namespace app\student\controller;

use app\student\model\studentModel;
use app\student\model\teacherModel;
use think\Db;

class Student extends \think\Controller
{
    //查询学生
    public function selectStudent()
    {
        if($this->request->isPost())
        {
             $c_name = [];
             $c_name = $this->request->post();
           // var_dump($c_name);die;
            $selectStudent = studentModel::selectStudentModel($c_name);
            $this->assign('selectStudent',$selectStudent);
            //如果没有值传过来则返回的查询按钮是一个空的
            //var_dump($c_name);die; 是一个数组，哪怕没有返回值返回的也是空数组，而查询需要的是字符串
            //var_dump($c_name['c_name']);die; 是一个字符串，没有返回值就是一个空的字符串
            $this->assign('c_name',$c_name['c_name'] ?? "");//查询页面需要字符串
            return $this->fetch('student/selectStudent');

        }else{
            $selectStudent = studentModel::selectStudentModel();
            $this->assign('selectStudent',$selectStudent);
            $this->assign('c_name',$c_name ?? "");
            return $this->fetch('student/selectStudent');
        }

    }

    //增加学生
    public function insertStudent()
    {
        if($this->request->isPost())
        {
            $insert = $this->request->post();
            //时间
            $insert['create_time'] = date('Y-m-d H:i:s');
            $result = studentModel::insertStudentModel($insert);
            if($result)
            {
                return $this->success('添加成功','selectStudent');
            }else{
                return $this->error('添加失败','insertStudent');
            }
        }else{
            //班级
            $selectClasses = studentModel::selectClasses();
            //科目
            $selectCourse = studentModel::selectCourse();
            $this->assign('selectClasses',$selectClasses);
            $this->assign('selectCourse',$selectCourse);
            return $this->fetch('insertStudent');
        }
    }

    //删除学生
    public function deleteStudent()
    {
        if($this->request->isGet())
        {
            $deleteStudent = $this->request->get();
            $result = studentModel::deleteStudentModel($deleteStudent);
            if($result)
            {
                return $this->success('删除成功','selectStudent');
            }else{
                return $this->error('删除失败','selectStudent');
            }

        }else{
            return $this->error('删除失败','selectStudent');
        }

    }

    //修改学生
    public function updateStudent()
    {
        if($this->request->isPost())
        {
            $update = $this->request->post();

            //把班级id，和科目id一同传进去，等到model层在分离
            $result = studentModel::updateStudent($update);
            if($result)
            {
                return $this->success('修改成功','selectStudent');
            }else{
                return $this->error('修改失败','selectStudent');
            }


        }else{

            $updateStudent_id = $this->request->get();
            //查询学生数据
            $getUpdate = studentModel::GetStudentModel($updateStudent_id);

            //查询班级数据
            $selectClasses = studentModel::selectClasses();

            //查询科目数据
            $selectCourse = studentModel::selectCourse();

            //把键名id换成 键名为s_id 这样才好去s_report表里面查值
            $selectRepoer_s_id['s_id'] = $updateStudent_id['id'];
            $selectRepoer = studentModel::selectReport($selectRepoer_s_id);

            //把从report成绩表求出的班级id抽取出来，单独成一个数组
            $classes_id = array_column($selectRepoer,'cls_id');

            //把从report成绩表求出的科目id抽取出来，单独成一个数组
            $course_id = array_column($selectRepoer,'c_id');

            //var_dump($classes_id,$selectRepoer,$course_id);

            //给学生数据赋值
            $this->assign('getUpdate',$getUpdate);

            //给班级数据赋值
            $this->assign('selectClasses',$selectClasses);

            //给科目数据赋值
            $this->assign('selectCourse',$selectCourse);

            //表示班级id
            $this->assign('classes_id',$classes_id);

            //表示科目id
            $this->assign('course_id',$course_id);

            return $this->fetch('updateStudent');

        }
    }

}